package com.feng.rabbitmq.three;

import com.feng.rabbitmq.utiils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Task2 {
    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        //发布确认
        channel.confirmSelect();
        /**
         * Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'ack_queue' in vhost '/': received 'true' but current is 'false', class-id=50, method-id=10)
         * 	at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:517)
         * 	at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:341)
         * 	at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:182)
         * 	at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:114)
         * 	at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:739)
         * 	at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47)
         * 	at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:666)
         * 	at java.lang.Thread.run(Thread.java:745)
         * 	错误原因： RabbitMQ中之前队列启动过了，并且之前的是非持久化的，现在修改持久化后，RabbitMQ中的没有删除导致启动报错
         * 	解决办法： 删除RabbitMQ中的队列，再启动
         */
        //队列是否持久化存储
        boolean durable = true;
        channel.queueDeclare(QUEUE_NAME,durable,false,false,null);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String message = scanner.next();
//            channel.basicPublish("",QUEUE_NAME,null,message.getBytes("utf-8"));
            //MessageProperties.PERSISTENT_TEXT_PLAIN: 消息持久化
            channel.basicPublish("",QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes("utf-8"));
            System.out.println("消息发送完毕："+message);
        }
    }
}
